package sensorrcp;

import java.io.PrintWriter;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.URL;
import java.util.Properties;
import org.apache.derby.iapi.services.property.PropertyUtil;

import org.apache.derby.drda.NetworkServerControl;
import org.apache.derby.impl.drda.NetworkServerControlImpl;

public class DerbyDBServerControl
{

	public DerbyDBServerControl(String[] args)
	{
		try 
		{
			NetworkServerControlImpl localNetworkServerControlImpl = new NetworkServerControlImpl();
			
		      int j = localNetworkServerControlImpl.parseArgs(args);
		      if (needsSecurityManager(localNetworkServerControlImpl, j))
		      {
		        verifySecurityState(localNetworkServerControlImpl);
		        installSecurityManager(localNetworkServerControlImpl);
		      }
		      int i = 0;
		      localNetworkServerControlImpl.executeWork(j);			
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	  public static final int DEFAULT_PORTNUMBER = 1527;
	  private static final String DERBYNET_JAR = "derbynet.jar";
	  private static final String POLICY_FILENAME = "server.policy";
	  private static final String POLICY_FILE_PROPERTY = "java.security.policy";
	  private static final String DERBY_HOSTNAME_WILDCARD = "0.0.0.0";
	  private static final String IPV6_HOSTNAME_WILDCARD = "::";
	  private static final String SOCKET_PERMISSION_HOSTNAME_WILDCARD = "*";
	  private NetworkServerControlImpl serverImpl;

	  public void start(PrintWriter paramPrintWriter)
	    throws Exception
	  {
	    this.serverImpl.start(paramPrintWriter);
	  }

	  public void shutdown()
	    throws Exception
	  {
	    this.serverImpl.shutdown();
	  }

	  public void ping()
	    throws Exception
	  {
	    this.serverImpl.ping();
	  }

	  public void trace(boolean paramBoolean)
	    throws Exception
	  {
	    this.serverImpl.trace(paramBoolean);
	  }

	  public void trace(int paramInt, boolean paramBoolean)
	    throws Exception
	  {
	    this.serverImpl.trace(paramInt, paramBoolean);
	  }

	  public void logConnections(boolean paramBoolean)
	    throws Exception
	  {
	    this.serverImpl.logConnections(paramBoolean);
	  }

	  public void setTraceDirectory(String paramString)
	    throws Exception
	  {
	    this.serverImpl.sendSetTraceDirectory(paramString);
	  }

	  public String getSysinfo()
	    throws Exception
	  {
	    return this.serverImpl.sysinfo();
	  }

	  public String getRuntimeInfo()
	    throws Exception
	  {
	    return this.serverImpl.runtimeInfo();
	  }

	  public void setMaxThreads(int paramInt)
	    throws Exception
	  {
	    this.serverImpl.netSetMaxThreads(paramInt);
	  }

	  public int getMaxThreads()
	    throws Exception
	  {
	    String str = this.serverImpl.getCurrentProperties().getProperty("derby.drda.maxThreads");
	    return Integer.parseInt(str);
	  }

	  public void setTimeSlice(int paramInt)
	    throws Exception
	  {
	    this.serverImpl.netSetTimeSlice(paramInt);
	  }

	  public int getTimeSlice()
	    throws Exception
	  {
	    String str = this.serverImpl.getCurrentProperties().getProperty("derby.drda.timeSlice");
	    return Integer.parseInt(str);
	  }

	  public Properties getCurrentProperties()
	    throws Exception
	  {
	    return this.serverImpl.getCurrentProperties();
	  }

	  protected void setClientLocale(String paramString)
	  {
	    this.serverImpl.clientLocale = paramString;
	  }

	  private static boolean needsSecurityManager(NetworkServerControlImpl paramNetworkServerControlImpl, int paramInt)
	    throws Exception
	  {
	    return ((System.getSecurityManager() == null) && (paramInt == 0) && (!(paramNetworkServerControlImpl.runningUnsecure())));
	  }

	  private static void verifySecurityState(NetworkServerControlImpl paramNetworkServerControlImpl)
	    throws Exception
	  {
	  }

	  private static void installSecurityManager(NetworkServerControlImpl paramNetworkServerControlImpl)
	    throws Exception
	  {
	    if (PropertyUtil.getSystemProperty("derby.system.home") == null)
	      System.setProperty("derby.system.home", PropertyUtil.getSystemProperty("user.dir"));
	    if (PropertyUtil.getSystemProperty("derby.drda.traceDirectory") == null)
	      System.setProperty("derby.drda.traceDirectory", PropertyUtil.getSystemProperty("derby.system.home"));
	    System.setProperty("derby.security.host", getHostNameForSocketPermission(paramNetworkServerControlImpl));
	    String str1 = getCodeSourcePrefix(paramNetworkServerControlImpl);
	    System.setProperty("derby.install.url", str1);
	    String str2 = getPolicyFileURL();
	    System.setProperty("java.security.policy", str2);
	    SecurityManager localSecurityManager = new SecurityManager();
	    System.setSecurityManager(localSecurityManager);
	    String str3 = paramNetworkServerControlImpl.localizeMessage("DRDA_SecurityInstalled.I", null);
	    paramNetworkServerControlImpl.consoleMessage(str3, true);
	  }

	  private static String getHostNameForSocketPermission(NetworkServerControlImpl paramNetworkServerControlImpl)
	    throws Exception
	  {
	    String str = paramNetworkServerControlImpl.getHost();
	    if ((hostnamesEqual("0.0.0.0", str)) || ("::".equals(str)))
	      str = "*";
	    else if (isIPV6Address(str))
	      str = '[' + str + "]:0-";
	    return str;
	  }

	  private static boolean hostnamesEqual(String paramString1, String paramString2)
	  {
	    try
	    {
	      InetAddress localInetAddress1 = InetAddress.getByName(paramString1);
	      InetAddress localInetAddress2 = InetAddress.getByName(paramString2);
	      return localInetAddress1.equals(localInetAddress2);
	    }
	    catch (Exception localException)
	    {
	    }
	    return false;
	  }

	  private static boolean isIPV6Address(String paramString)
	  {
	    if (paramString == null)
	      return false;
	    int i = paramString.length();
	    for (int j = 0; j < i; ++j)
	    {
	      char c = paramString.charAt(j);
	      if (c == ':')
	        continue;
	      if (Character.digit(c, 16) < 0)
	        return false;
	    }
	    try
	    {
	      InetAddress localInetAddress = InetAddress.getByName(paramString);
	      return localInetAddress instanceof Inet6Address;
	    }
	    catch (Exception localException)
	    {
	    }
	    return false;
	  }

	  private static String getCodeSourcePrefix(NetworkServerControlImpl paramNetworkServerControlImpl)
	    throws Exception
	  {
	    String str1 = NetworkServerControl.class.getProtectionDomain().getCodeSource().getLocation().toExternalForm();
	    int i = str1.indexOf("derbynet.jar");
	    if (i < 0)
	    {
	      String str2 = paramNetworkServerControlImpl.localizeMessage("DRDA_MissingNetworkJar.S", null);
	      paramNetworkServerControlImpl.consoleError(str2);
	    }
	    String str2 = str1.substring(0, i);
	    return str2;
	  }

	  private static String getPolicyFileURL()
	    throws Exception
	  {
	    String str1 = NetworkServerControl.class.getPackage().getName().replace('.', '/') + '/' + "server.policy";
	    URL localURL = NetworkServerControl.class.getClassLoader().getResource(str1);
	    String str2 = localURL.toExternalForm();
	    return str2;
	  }

}
